home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 14642 / 14642.xpi / chrome / content / historyCategory.js < prev    next >
Text File  |  2009-09-29  |  12KB  |  341 lines

  1. /* Copyright 2009, Boomtango.com.  All Rights Reserved. */
  2. /* historyCategory.js
  3.  * Responsible for category view
  4.  */
  5.  
  6. bthistory.controllers["category"] = {
  7.     onHistoryAdd: function(dataset) {
  8.         if(!document.getElementById("column1")){
  9.             bthistory.updateView();
  10.             return;
  11.         }
  12.         bthistory.app.debug("category::onHistoryAdd");
  13.         var len = dataset.length;
  14.         var max = bthistory.app.glanceItemsPerPage;
  15.         for(var x = 0; x < len; x++){
  16.             var item = dataset[x];
  17.             if(item.type == "web" && len > 1){
  18.                 break;
  19.             }
  20.             var box = document.getElementById("categorybox." + item.type);
  21.             if(!box){
  22.                 var column = document.getElementById(
  23.                         this.nextColumn ? "column1" : "column2");
  24.                 this.loadCategory(item.type, [ item ], column, max);                
  25.                 this.nextColumn = !this.nextColumn;
  26.             } else {
  27.                 var hbox = this.buildLineItemBox(item.type, item);
  28.                 if(box.childNodes.length < max + 1){ 
  29.                     box.insertBefore(hbox, box.firstChild.nextSibling);
  30.                 } else if(box.childNodes.length == max + 1){ 
  31.                     box.insertBefore(hbox, box.firstChild.nextSibling);
  32.                     
  33.                     var hbox = this.buildMoreItemsBox(item.type);
  34.                     box.appendChild(hbox);
  35.                 } else {
  36.                     box.insertBefore(hbox, box.firstChild.nextSibling);
  37.                     box.removeChild(box.lastChild.previousSibling);
  38.                 }
  39.             }
  40.         }
  41.  
  42.     },
  43.     onHistoryChange: function(data) {
  44.         bthistory.app.debug("category::onHistoryChange");
  45.     },
  46.     handleUpArrow: function(){
  47.         var sels = document.getElementsByClassName("selected");
  48.         var body = document.getElementById("body");
  49.         var items = document.getElementsByClassName("historyItem");
  50.         if(items.length == 0){
  51.             return;
  52.         }
  53.         if(sels.length == 0){
  54.             var node = items[items.length - 1].parentNode;
  55.             bthistory.selectNode(node);
  56.             bthistory.scrollIntoView(body, node, 24);
  57.         } else {
  58.             var sel = sels[0];
  59.             var len = items.length;
  60.             for(var x = 0; x < len; x++){
  61.                 if(sel == items[x].parentNode){
  62.                     if(x > 0){
  63.                         var node = items[x - 1].parentNode;
  64.                         bthistory.selectNode(node);
  65.                         bthistory.scrollIntoView(body, node, 24);
  66.                     }
  67.                     break;
  68.                 }
  69.             }
  70.         }
  71.     },
  72.     handleDownArrow: function(){
  73.         var sels = document.getElementsByClassName("selected");
  74.         var body = document.getElementById("body");
  75.         var items = document.getElementsByClassName("historyItem");
  76.         if(items.length == 0){
  77.             return;
  78.         }
  79.         if(sels.length == 0){
  80.             var node = items[0].parentNode;
  81.             bthistory.selectNode(node);
  82.             bthistory.scrollIntoView(body, node, 24);
  83.         } else {
  84.             var sel = sels[0];
  85.             var len = items.length;
  86.             for(var x = 0; x < len; x++){
  87.                 if(sel == items[x].parentNode){
  88.                     if(x + 1 < len){
  89.                         var node = items[x + 1].parentNode;
  90.                         bthistory.selectNode(node);
  91.                         bthistory.scrollIntoView(body, node, 24);
  92.                     }
  93.                     break;
  94.                 }
  95.             }
  96.         }
  97.     },
  98.     handleResize: function(){
  99.     },
  100.     queryTracker: function(types, filter){
  101.         // usually web means all but for category, we really need all
  102.         if(types.length == 1 && types[0] == 'web'){
  103.             var alltypes = bthistory.app.tracker.types;
  104.             types = [];
  105.             for(var x in alltypes){
  106.                 types.push(x);
  107.             }
  108.         }
  109.  
  110.         return bthistory.storage.queryTrackerByCategory(
  111.             bthistory._range.start,
  112.             bthistory._range.end,
  113.             types,
  114.             filter,
  115.             bthistory.app.glanceItemsPerPage + 1,
  116.             false
  117.         );
  118.     },
  119.     loadView: function(){
  120.         var data = bthistory._data;
  121.         var types = bthistory.tracker.types;
  122.         var hasdata = false;
  123.         for(var x in types){
  124.             if(data.hasOwnProperty(x)){
  125.                 hasdata = true;
  126.                 break;
  127.             }
  128.         }
  129.         if(!hasdata){
  130.             var body = document.getElementById("body");
  131.             var label= document.createElement("label");
  132.             label.setAttribute("value", bthistory.app.getString("history.nodatafound"));
  133.             label.className = "nodatafound";
  134.             body.appendChild(label);
  135.         } else {
  136.             document.loadOverlay("chrome://boomtango/content/historyGlance.xul", this);
  137.         }
  138.         document.getElementById("bubble_back").setAttribute("hidden", "false");
  139.     },
  140.     observe: function(subject, topic, data) {
  141.         if(topic == "xul-overlay-merged"){
  142.             bthistory.app.log("historyGlance::overlayLoaded");
  143.             this.loadMergedView();
  144.         }
  145.     },
  146.     /*
  147.         loadMergedView is handled after view has been merged.  
  148.     */
  149.     loadMergedView: function() {
  150.         var body = document.getElementById("body");
  151.         body.setAttribute('style', 'border: none; padding: none;');
  152.         var data = bthistory._data;
  153.         var leftColumn = true;
  154.         var types = bthistory.tracker.types;
  155.         var max = bthistory.app.glanceItemsPerPage;
  156.         for(var x in types){
  157.             if(data.hasOwnProperty(x) && bthistory.app.getTrackerEnabled(x)){
  158.                 var column = document.getElementById(
  159.                         leftColumn ? "column1" : "column2");
  160.                 this.loadCategory(x, data[x], column, max);                
  161.                 leftColumn = !leftColumn;
  162.             }
  163.         }
  164.         this.nextColumn = leftColumn;
  165.         document.getElementById("body_header").
  166.            setAttribute("hidden", "false");
  167.         // we've added stuff above the body element so we need a recalc
  168.         bthistory.resizeToWindow();
  169.     },
  170.     buildMoreItemsBox: function(type){
  171.         var hbox = document.createElement('hbox');
  172.         hbox.setAttribute("flex", "1");
  173.         var spacer = document.createElement('spacer');
  174.         spacer.setAttribute("flex", "1");
  175.         hbox.appendChild(spacer);
  176.  
  177.         var linkNode = document.createElement("label");
  178.         linkNode.className = "text-link";
  179.         linkNode.setAttribute("value", bthistory.app.getString("category.moreitems"));
  180.         linkNode.setAttribute("crop", "end");
  181.         linkNode.addEventListener(
  182.             "click",
  183.             function(){
  184.                 bthistory.handleMoreItems(type);
  185.             },
  186.             false
  187.         );
  188.         hbox.appendChild(linkNode);
  189.         return hbox;
  190.     },
  191.     buildLineItemBox: function(type, item){
  192.         var hbox = document.createElement('hbox');
  193.         var fi = Components.classes["@mozilla.org/browser/favicon-service;1"].getService(Components.interfaces.nsIFaviconService);
  194.         var io = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService2);
  195.         hbox.setAttribute("flex", "1");
  196.         hbox.setAttribute("contentID", item.ftsrowid);
  197.         hbox.setAttribute("trackerID", item.id);
  198.         var uri = io.newURI(item.url, null, null); 
  199.         var iconURI = fi.getFaviconImageForPage(uri);
  200.         var img = document.createElement("image");
  201.         img.setAttribute("src", iconURI.spec);
  202.         img.className = "calendaricon";
  203.         var title = item.title || item.url;
  204.         var vbox = document.createElement('vbox');
  205.         var linkNode = document.createElement("label");
  206.         linkNode.className = "historyItem";
  207.         linkNode.setAttribute("value", title);
  208.         linkNode.setAttribute("crop", "end");
  209.         linkNode.setAttribute("flex", "1");
  210.         vbox.appendChild(img);
  211.         hbox.appendChild(vbox);
  212.         hbox.appendChild(linkNode);
  213.  
  214.         label = document.createElement("label");
  215.         label.className = "text-link";
  216.         label.setAttribute("value", bthistory.datestring(item.starttime));
  217.         this.addStartClickHandler(label, item);
  218.         hbox.appendChild(label);
  219.         
  220.         /*
  221.         vbox = document.createElement('vbox');
  222.         vbox.className = "closeBox";
  223.         vbox.id = "closebox." + item.ftsrowid;
  224.         vbox.style.visibility = "hidden";
  225.         vbox.addEventListener(
  226.             "click",
  227.             function(){
  228.                 bthistory.handleDeleteHistoryItem(hbox, item.ftsrowid);
  229.             },
  230.             false
  231.         );
  232.         
  233.         hbox.addEventListener(
  234.             "mouseover",
  235.             function(e){
  236.                 var node = e.target;
  237.                 
  238.                 var id = node.getAttribute ? node.getAttribute("contentID") : null;
  239.                 while(!id){
  240.                     node = node.parentNode;
  241.                     if(!node || node.id == "body" ||  !node.getAttribute){
  242.                         break;
  243.                     }
  244.                     id = node.getAttribute("contentID")
  245.                 }
  246.                     
  247.                 if(id){
  248.                     var closebox = document.getElementById("closebox." + id);
  249.                     
  250.                     if (closebox) {
  251.                         closebox.style.visibility = "visible";
  252.                     }
  253.                 }
  254.             },
  255.             false
  256.         );
  257.         hbox.addEventListener(
  258.             "mouseout",
  259.             function(e){
  260.                 var node = e.target;
  261.                 
  262.                 var id = node.getAttribute ? node.getAttribute("contentID") : null;
  263.                 while(!id){
  264.                     node = node.parentNode;
  265.                     if(!node || node.id == "body" ||  !node.getAttribute){
  266.                         break;
  267.                     }
  268.                     id = node.getAttribute("contentID")
  269.                 }
  270.                     
  271.                 if(id){
  272.                     var closebox = document.getElementById("closebox." + id);
  273.                     
  274.                     if (closebox) {
  275.                         closebox.style.visibility = "hidden";
  276.                     }
  277.                 }
  278.             },
  279.             false
  280.         );
  281.         
  282.         img = document.createElement("image");
  283.         img.setAttribute("src", "chrome://boomtango/skin/close_icon.png");
  284.         vbox.appendChild(img);
  285.         hbox.appendChild(vbox);
  286.         */
  287.         
  288.         return hbox;
  289.     },
  290.     loadCategory: function(type, data, container, max){
  291.         if(data && data.length){
  292.             var name = type == "web" ? bthistory.app.getString("category.type.all")
  293.                  : bthistory.app.tracker.types[type].name_plural;
  294.             var len = data.length;
  295.             var box = document.createElement("vbox");
  296.             box.className = "categorybox";
  297.             box.id = "categorybox." + type;
  298.             box.setAttribute("style", "border-color: " +
  299.                     bthistory.app.getTrackerColor(type) + ";");
  300.  
  301.             var label = document.createElement("label");
  302.             label.className = "topurl_title";
  303.             label.setAttribute("value", name);
  304.             box.appendChild(label);
  305.  
  306.             for(var x = 0; x < data.length; x++){
  307.                 if(x >= max){
  308.                     var hbox = this.buildMoreItemsBox(type);
  309.                     box.appendChild(hbox);
  310.                     break;
  311.                 }
  312.                 var item = data[x];
  313.                 var hbox = this.buildLineItemBox(type, item);
  314.                 box.appendChild(hbox);
  315.             }
  316.             
  317.             container.appendChild(box);
  318.         }
  319.     },
  320.     addStartClickHandler: function(linkNode, item){
  321.         linkNode.addEventListener(
  322.             "click",
  323.             function(){
  324.                 bthistory.currTime = item.starttime;
  325.                 bthistory.loadView('calendar', 'hour', item.ftsrowid);
  326.             },
  327.             false
  328.         );
  329.     },
  330.     
  331.     
  332.     QueryInterface: function(iid) {  
  333.         if (iid.equals(bthistory.app.ci.nsIObserver) ||  
  334.                iid.equals(bthistory.app.ci.nsISupports)) {  
  335.             return this;  
  336.         } 
  337.         throw Components.result.NS_ERROR_NO_INTERFACE;  
  338.     }
  339.  
  340. };
  341.